package com.tbit.uqbike.service.business.tag.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tbit.config.datasource.DataSource;
import com.tbit.uqbike.object.pojo.tag.TagNew;
import com.tbit.uqbike.service.base.RedisService;
import com.tbit.uqbike.service.business.tag.dao.TagDaoNew;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author: qiu.longjie
 * @create: 2025-05-08
 */
@Slf4j
@Service
public class TagServiceNew extends ServiceImpl<TagDaoNew, TagNew> {
    @Autowired
    RedisService redisService;

    @DS(DataSource.CORE)
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public List<TagNew> getTags(Integer machineId) {
        String ids = redisService.get(TagServiceImpl.DISPATCH_CURR_TAG_KEY + machineId);
        if (ids == null) {
            return Collections.emptyList();
        }
        Set<Long> idSet = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .collect(Collectors.toSet());
        return list(Wrappers.<TagNew>query().in("id", idSet));
    }
}
